<template>
  <div>
    <div class="d_header">
      <div class="d_header_left">
        <a @click="goback" href="javascript:;">
          <img class="typeCake" src="../assets/images/left_go.png" alt="" />
        </a>
        <a @click="goback" href="javascript:;" class="fanhui">返回</a>
      </div>

      <div class="d_header_middle">登录</div>
    </div>
    <div class="form">
      <van-form validate-first @submit="checkForm">
        <!-- 通过 validator 进行函数校验 -->
        <van-field label="账号：" v-model="uname" placeholder="请输入账号" />
        <!-- 通过 validator 进行函数校验 -->
        <van-field
          label="密码："
          type="password"
          v-model="upwd"
          placeholder="请输入密码"
        />
        <!-- 快速注册 -->
        <div class="to_register">
          <span v-show="loginState">账号或密码错误，请重试</span>
          <router-link to="/register">没有账号？快速注册&nbsp;></router-link>
        </div>
        <!-- 通过 validator 进行异步函数校验 -->
        <div class="login_btn" style="margin: 16px">
          <van-button
            color="#ffd100"
            round
            block
            type="info"
            native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
// vmap：利用此方式可以看提示
// 辅助函数：帮你快速引入mutations中的方法
import { mapMutations } from "vuex";

export default {
  data() {
    return {
      uname: "",

      upwd: "",

      loginState: false,
    };
  },
  methods: {
    ...mapMutations(["updateUname", "updateIsloginState", "updateUid"]),
    goback() {
      this.$router.go(-1);
    },
    checkForm() {
      // console.log(123);
      let uname = this.uname;
      let upwd = this.upwd;
      this.axios
        .post("/user/login", `uname=${uname}&upwd=${upwd}`)
        .then((res) => {
          if (res.data.code == 200) {
            let uid = res.data.loginResult.uid;
            let uname = res.data.loginResult.uname;
            let unickname = res.data.loginResult.unickname;
            let upwd = res.data.loginResult.upwd;
            let gender = res.data.loginResult.gender;
            let avatar_pic = res.data.loginResult.avatar_pic;
            sessionStorage.setItem("islogin", true);
            sessionStorage.setItem("unickname", unickname);

            sessionStorage.setItem("uid", uid);

            sessionStorage.setItem("uname", uname);
            sessionStorage.setItem("upwd", upwd);
            sessionStorage.setItem("gender", gender);
            sessionStorage.setItem("avatar_pic", avatar_pic);
            // 将vuex中的unickname赋值为从后端拿到的昵称
            this.updateUname(unickname);

            this.updateIsloginState(true);
            this.updateUid(uid);
            console.log(res.data);
            this.loginState = false;
            this.$toast({
              message: "登录成功！",
              forbidClick: true,
              type: "success",
              duration: 3000,
              onClose: () => {
                this.$router.push("/");
              },
            });
          } else {
            this.loginState = true;
            console.log("login failed");
          }
        });
    },
  },
};
</script>
<style scoped src="../assets/css/login.css"></style>
<style lang="scss" scoped></style>
